package com.sinoglobal.dumping.dumplingdialog;

/**
 * Created by KeZhuang on 2016/1/14.
 */

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.ImageView;

import com.sinoglobal.dumping.activity.Dumpling_MainActivity;
import com.sinoglobal.dumping.activity.Dumpling_NoPasswordQuicklyLoginActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoConfig;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.bean.Dumpling_LogOutAcquireDumpling;
import com.sinoglobal.dumping.bean.Dumpling_LogOutAcquireDumplingBean;
import com.sinoglobal.dumping.bean.Dumpling_ShareInfoResultList;
import com.sinoglobal.dumping.fragment.Dumpling_HomeFragment;
import com.sinoglobal.dumping.fragment.helper.Dumpling_HomefragmentImpl;
import com.sinoglobal.dumping.util.DumplingCache;
import com.sinoglobal.dumping.util.Dumpling_HttpMethodSet;
import com.sinoglobal.dumping.util.DumplingUtils;
import com.sinoglobal.dumping.view.Dumpling_GetMoneyDialog;
import com.sinoglobal.wallet.activity.W_HomeActivity;

import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.DUMPLING_MARK_SHARE;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.LOGIN_STATE_LOGIN;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.TYPE_DUMPLING_COUPON_3;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.TYPE_DUMPLING_COUPON_5;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.TYPE_DUMPLING_COUPON_6;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.TYPE_DUMPLING_COUPON_7;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.TYPE_DUMPLING_MONEY;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.TYPE_DUMPLING_WISH;
import static com.sinoglobal.dumping.base.Dumpling_SinoValueManager.putValue;
public class DumplingDialogHelper {
    private Context context;
    private ImageView imageview;
    private Dumpling_LogOutAcquireDumplingBean dumplingBean;
    private Dumpling_LogOutAcquireDumpling dumpling;
    double getMoney() {return DumplingUtils.getMoney(context);}
    int getCoupon() {return DumplingUtils.getCoupon(context);}
    int getCount() {return  DumplingUtils.getCount(context);}
    int getShare() {return DumplingUtils.getShare(context);}
    Dumpling_LogOutAcquireDumpling getDumplingVo(){return dumpling;}
    void showToast(String message){((Dumpling_MainActivity)context).showToast(message);}
    boolean getLogin() {return DumplingUtils.getLogin(context) == LOGIN_STATE_LOGIN;}
    Context getContext(){return context;}
    void leftButtonClick(){
        if (getLogin()) {
            goShare();
        } else {
            getDumpling(context);
        }
    }
    Dumpling_GetMoneyDialog.Builder getBuilder() {
        double dumplingMoney = getMoney();
        int couponCount = getCoupon();
        Dumpling_GetMoneyDialog.Builder builder = new Dumpling_GetMoneyDialog.Builder(context);
        builder.setImageView(imageview)
                .setToast("还有" + getCount() + "次机会")
                .setTitle(dumplingBean.getResultList().getDumpling().getPutUser())
                .setImgUrl(dumplingBean.getResultList().getDumpling().getUserImg())
                .setContent(dumplingMoney != 0.0 && couponCount != 0 ? "今日共捞到" + dumplingMoney + "元," + couponCount + "张优惠券" :
                        dumplingMoney != 0.0 && couponCount == 0 ? "今日共捞到" + dumplingMoney + "元" :
                                dumplingMoney == 0.0 && couponCount != 0 ? "今日共捞到" + couponCount + "张优惠券" : ""
                );
        return builder;
    }
    void goShare() {
        if (Dumpling_SinoConfig.sMoneyShare != null || Dumpling_SinoConfig.sCouponShare!=null ) {
            Dumpling_ShareInfoResultList resultList = null;
            if(Dumpling_SinoConfig.sMoneyShare!=null){
                resultList = Dumpling_SinoConfig.sMoneyShare.getResultList();
                Dumpling_SinoConfig.sMoneyShare = null;
            }
            if(Dumpling_SinoConfig.sCouponShare!=null){
                resultList = Dumpling_SinoConfig.sCouponShare.getResultList();
                Dumpling_SinoConfig.sCouponShare = null;
            }
            if(resultList!=null){
                ((Dumpling_MainActivity) context).setShare(
                        resultList.getContent(),
                        resultList.getTitle(), null, resultList.getUrl() + "?productCode=" + Dumpling_SinoConstans.PRODUCT_CODE, 0, resultList.getPicurl());
            }
        }
        Dumpling_HttpMethodSet.markShare(context, Dumpling_SinoConstans.USER_ID);
        int makeShareCount = getShare();
        makeShareCount++;
        putValue(context, DUMPLING_MARK_SHARE, makeShareCount, true);
    }
    void loginAcquireDumpling(){((Dumpling_HomefragmentImpl)getFragment().getHelper()).getAnimationManager().acquireloginUserDumpling();}
    void acquireDumpling(){getFragment().helper.createDumplingAnim();}
    void goWallet() {((Dumpling_MainActivity) context).goIntent(W_HomeActivity.class);}
    Dumpling_HomeFragment getFragment() {return (Dumpling_HomeFragment) ((Dumpling_MainActivity) context).getFragmentByTag("0");}
    void getDumpling(Context context) {
        if (getLogin()) {
            ((Dumpling_MainActivity) context).showToast("正在为您领取饺子呀~");
            Dumpling_HttpMethodSet.logoutUserGetMoney(context);
        } else {
            Dumpling_HttpMethodSet.isGetMoney = !(getMoney() == 0.0 && getCoupon() == 0);
            ((Dumpling_MainActivity)context).goIntent(Dumpling_NoPasswordQuicklyLoginActivity.class);
        }
    }
    public static void getDumpling_(Context context){
        DumplingDialogHelper helper = new DumplingDialogHelper();
        helper.context = context;
        helper.getDumpling(context);
    }
    public static Dumpling_GetMoneyDialog getDialog(Activity context,Dumpling_LogOutAcquireDumplingBean dumplingBean) {
        DumplingDialogHelper helper = new DumplingDialogHelper();
        helper.context = context;
        helper.imageview = helper.getFragment().ivDumpling;
        helper.dumplingBean = dumplingBean;
        helper.dumpling = dumplingBean.getResultList().getDumpling();
        int type = Integer.parseInt(dumplingBean.getResultList().getDumpling().getDumplingType());
        return helper.getFactory(type).makeDialog(helper);
    }
    void setValue(DumplingDialogHelper helper){
        context = helper.context;
        imageview = helper.imageview;
        dumplingBean = helper.dumplingBean;
        dumpling = helper.dumpling;
    }
    DumplingIDialogFactory getFactory(int type) {
        switch (type) {
            case TYPE_DUMPLING_MONEY:
                return new DumplingMoneyDialog();
            case TYPE_DUMPLING_WISH:
                return new DumplingWishDialog();
            case TYPE_DUMPLING_COUPON_3:
            case TYPE_DUMPLING_COUPON_5:
            case TYPE_DUMPLING_COUPON_6:
            case TYPE_DUMPLING_COUPON_7:
                return new DumplingCouponDialog();
        }
        return null;
    }
}
